package algorithm

import "fmt"

type ListNode struct {
	Val  int
	Next *ListNode
}

func (ll ListNode) prinfListInOrder() {
	fmt.Print("->", ll.Val)
	if ll.Next != nil {
		ll.Next.prinfListInOrder()
	}
}
func reverse(head *ListNode) *ListNode {
	if head == nil || head.Next == nil {
		return head
	}
	var cur, pre *ListNode = nil, nil
	for head != nil {
		cur = head.Next
		head.Next = pre
		pre = head
		head = cur

	}
	return pre
}
func RunReverseList() {
	head := new(ListNode)
	head.Val = 1
	ln2 := new(ListNode)
	ln2.Val = 2
	ln3 := new(ListNode)
	ln3.Val = 3
	ln4 := new(ListNode)
	ln4.Val = 4
	ln5 := new(ListNode)
	ln5.Val = 5
	head.Next = ln2
	ln2.Next = ln3
	ln3.Next = ln4
	ln4.Next = ln5
	fmt.Println("运行算法题，反转链表！！！")

	fmt.Println(map[string]interface{}{
		"初始链表": head,
	})
	head.prinfListInOrder()
	pre := reverse(head)
	fmt.Println()
	fmt.Println(map[string]interface{}{
		"翻转后的链表": pre,
	})
	pre.prinfListInOrder()
}
